//
//  WBHomeOrigina.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/8.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit
import SnapKit

class WBHomeOrigina: UIView {
    
    var  retweetBottomContraint: Constraint?
    
    var statusViewModel:WBStatusViewModel? {
        
        didSet {
            
            guard let currentViewModel = statusViewModel else {
                
                return
            }
            if let imgUrl = statusViewModel?.status?.user?.profile_image_url {
                
                userImageView.sd_setImage(with: URL(string:imgUrl), placeholderImage: UIImage(named:"avatar_default_big"))
                
            }
            screenNameLabel.text = currentViewModel.status?.user?.screen_name
            contenLabel.attributedText = currentViewModel.originalAttributedText
            mbrankImageView.image = currentViewModel.mbrankImahe
            verifiedTypeImageView.image = currentViewModel.verifiedTypeImage
            sourceLabel.text = currentViewModel.sourceContent
            timeLabel.text = currentViewModel.timeContent
            
            retweetBottomContraint?.deactivate()
            
            if let picUrls = currentViewModel.status?.pic_urls, picUrls.count > 0 {
                
                pictureView.picUrls = picUrls
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                    
                    retweetBottomContraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                })
                
            } else {
                
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    
                    retweetBottomContraint = make.bottom.equalTo(contenLabel).offset(HomeCellMargin).constraint
                    
                })
            }

            
        }
    }
    
    // 用户头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    // 认证类型图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named:"avatar_vip"))
    // 昵称
    private lazy var screenNameLabel: UILabel = {
       
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "hehe"
        
        return label
    }()
    // 时间
    private lazy var timeLabel: UILabel = {
       
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        
        return label
    }()
    // 来源
    private lazy var sourceLabel: UILabel = {
       
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 新浪微博"
        
        return label
    }()
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 微博内容
    private lazy var contenLabel: UILabel = {
        
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦"
        label.numberOfLines = 0
        
        return label
    }()
    // 配图
    private lazy var pictureView: WBHomePictureView = {
       
        let collectionView = WBHomePictureView()
        collectionView.backgroundColor = self.backgroundColor
        
        return collectionView
    }()
    // 初始化方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        
        
        addSubview(userImageView)
        userImageView.snp.makeConstraints { (make) in
            
            make.top.left.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
        
        addSubview(verifiedTypeImageView)
        verifiedTypeImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(userImageView.snp.right)
            make.centerY.equalTo(userImageView.snp.bottom)
            
        }
        
        addSubview(screenNameLabel)
        screenNameLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView)
            make.left.equalTo(userImageView.snp.right).offset(HomeCellMargin)
            
        }
        
        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(userImageView)
            make.left.equalTo(screenNameLabel)
            
        }
        
        addSubview(sourceLabel)
        sourceLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp.right).offset(HomeCellMargin)
            
        }
        
        addSubview(mbrankImageView)
        mbrankImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(screenNameLabel)
            make.left.equalTo(screenNameLabel.snp.right).offset(HomeCellMargin)
            
        }
        
        addSubview(contenLabel)
        contenLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.left.equalTo(userImageView)
            make.right.equalTo(self).offset(-10)
            
        }
        
        addSubview(pictureView)
        pictureView.snp.makeConstraints { (make) in
            
            make.left.equalTo(contenLabel)
            make.top.equalTo(contenLabel.snp.bottom).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }

        
        self.snp.makeConstraints { (make) in
            
         retweetBottomContraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
    

}
